package tmall.portal.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tmall.framework.exception.TException;
import tmall.framework.response.ResponseResult;
import tmall.framework.utils.RedisTemplateUtil;
import tmall.portal.dict.PortalExceptionDict;
import tmall.portal.eobj.UserEobj;
import tmall.portal.service.UserService;
import tmall.portal.utils.ArgumentUtil;
import tmall.security.exception.TSecurityException;
import tmall.security.utils.JwtTokenUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Path;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/rest/service/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.header}")
    private String tokenHeader;
    @Value("${jwt.expiration}")
    private Long expiration;

    @PostMapping("/login")
    public ResponseResult login(@RequestBody UserEobj userEobj, HttpServletResponse response){
        ArgumentUtil.isEmpty(userEobj);
        if(StringUtils.isEmpty(userEobj.getUsername())||StringUtils.isEmpty(userEobj.getPassword())){
            throw new TException(PortalExceptionDict.USERNAME_OR_PASSWORD_ABSENT);
        }
        UserEobj user = userService.queryUserByName(userEobj.getUsername());
        if(user == null){
            throw new TSecurityException(PortalExceptionDict.USERNAME_OR_PASSWORD_ERROR);
        }
        if(!passwordEncoder.matches(userEobj.getPassword(),user.getPassword())){
            throw new TSecurityException(PortalExceptionDict.USERNAME_OR_PASSWORD_ERROR);
        }
        String token = jwtTokenUtil.generateToken(user);
        response.setHeader(tokenHeader,token);
        RedisTemplateUtil.set(token, userEobj.getUsername(),expiration);
        return ResponseResult.success(getUserMap(user));

    }
    @PostMapping("/logout")
    public ResponseResult logout(HttpServletRequest request){
        String header =  request.getHeader(tokenHeader);
        if(StringUtils.isEmpty(header)){
            throw new TSecurityException(PortalExceptionDict.NO_TOKEN_TO_LOGOUT);
        }
        RedisTemplateUtil.remove(header);
        return ResponseResult.success(true);

    }
    private Map<String,Object> getUserMap(UserEobj userEObj){
        Map<String,Object> userMap = new HashMap<>();
        userMap.put("userId",userEObj.getUserId());
        userMap.put("userName",userEObj.getUsername());
        return userMap;
        //userMap.put("",userEObj.get)
    }
    @PostMapping("/registerUser")
    public ResponseResult registerUser(UserEobj userEobj){
        ArgumentUtil.isEmpty(userEobj);
        ArgumentUtil.isEmpty(userEobj.getUsername());
        ArgumentUtil.isEmpty(userEobj.getPassword());
        return ResponseResult.success(userService.registerUser(userEobj));
    }
    @PostMapping("/deleteUser/{id}")
    public ResponseResult deleteUser(@PathVariable long id){

        return ResponseResult.success(userService.deleteUser(id));
    }

}
